﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    // NOTE: If you change the class name "LaterInsurance" here, you must also update the reference to "LaterInsurance" in App.config.
    public partial class MainService : ILaterInsurance
    {

        public DataSet LaterInsurance_GetALL()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LaterInsurance_GetALL", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool LaterInsurance_CheckEmployee(int EmployeeID, int month, int year)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LaterInsurance_CheckEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            try
            {
                dbConn.Open();
                int Count = Convert.ToInt32(dbCmd.ExecuteScalar());
                retVal = Count > 0 ? true : false;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet LaterInsurance_GetForTrichNop(int month, int year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LaterInsurance_GetForTrichNop", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Month", month);
            dbCmd.Parameters.AddWithValue("@Year", year);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public LaterInsuranceInfo LaterInsurance_GetMonthWithEmployee(int EmployeeID, int month, int year)
        {
            LaterInsuranceInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LaterInsurance_GetMonthWithEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new LaterInsuranceInfo();
                    retVal.Date = Convert.ToDateTime(dr["LaterInsurance_Date"]);
                    retVal.MonthChange = Convert.ToDateTime(dr["LaterInsurance_MonthChange"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet LaterInsurance_TruyDong( int month, int year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LaterInsurance_TruyDong", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet LaterInsurance_GetList(out int itemCount, int currPage, int pageSize,int BranchID, int DepartmentID, int month, int year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LaterInsurance_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                itemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public LaterInsuranceInfo LaterInsurance_GetInfo(int LaterInsuranceID)
        {
            LaterInsuranceInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LaterInsurance_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LaterInsurance_ID", LaterInsuranceID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new LaterInsuranceInfo();
                    retVal.ID = Convert.ToInt32(dr["LaterInsurance_ID"]);
                    retVal.DepartmentID = Convert.ToInt32(dr["LaterInsurance_DepartmentID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["LaterInsurance_EmployeeID"]);
                    retVal.PositionID = Convert.ToInt32(dr["LaterInsurance_PositionID"]);
                    retVal.BasicFactor = Convert.ToDecimal(dr["LaterInsurance_BasicFactor"]);
                    retVal.PositionFactor = Convert.ToDecimal(dr["LaterInsurance_PositionFactor"]);
                    retVal.OverSeniorityFactor = Convert.ToDecimal(dr["LaterInsurance_OverSeniorityFactor"]);
                    retVal.SeniorityFactor = Convert.ToDecimal(dr["LaterInsurance_SeniorityFactor"]);
                    retVal.BasicSalary = Convert.ToDecimal(dr["LaterInsurance_Basic"]);
                    retVal.PositionAllowance = Convert.ToDecimal(dr["LaterInsurance_Position"]);
                    retVal.OverSeniorityAllowance = Convert.ToDecimal(dr["LaterInsurance_OverSeniority"]);
                    retVal.SeniorityAllowance = Convert.ToDecimal(dr["LaterInsurance_Seniority"]);
                    retVal.CompanySalary = Convert.ToDecimal(dr["LaterInsurance_Company"]);
                    retVal.CompanyFactor = Convert.ToDecimal(dr["LaterInsurance_CompanyFactor"]);
                    retVal.ResponseSalary = Convert.ToDecimal(dr["LaterInsurance_Response"]);
                    retVal.ResponseFactor = Convert.ToDecimal(dr["LaterInsurance_ResponseFactor"]);
                    retVal.TotalDateWork = Convert.ToInt32(dr["LaterInsurance_TotalDateWork"]);
                    retVal.TotalDateOutWork = Convert.ToInt32(dr["LaterInsurance_TotalDateOutWork"]);
                    retVal.TotalLeavePaid = Convert.ToInt32(dr["LaterInsurance_TotalLeavePaid"]);
                    retVal.TotalLeaveUnPaid = Convert.ToInt32(dr["LaterInsurance_TotalLeaveUnPaid"]);
                    retVal.TradeUnion = Convert.ToDecimal(dr["LaterInsurance_TradeUnion"]);
                    retVal.IncomeTax = Convert.ToDecimal(dr["LaterInsurance_IncomeTax"]);
                    retVal.OtherResponse = Convert.ToDecimal(dr["LaterInsurance_OtherResponse"]);
                    retVal.OtherDeduction = Convert.ToDecimal(dr["LaterInsurance_OtherDeduction"]);
                    retVal.CompanyLunch = Convert.ToDecimal(dr["LaterInsurance_Lunch"]);
                    retVal.TelephoneCost = Convert.ToDecimal(dr["LaterInsurance_TelephoneCost"]);
                    retVal.OverTime = Convert.ToDecimal(dr["LaterInsurance_OverTime"]);
                    retVal.Date = Convert.ToDateTime(dr["LaterInsurance_Date"]);
                    retVal.ProbationFactor = Convert.ToDecimal(dr["LaterInsurance_ProbationFactor"]);
                    retVal.EmployeeSocialInc = Convert.ToDecimal(dr["LaterInsurance_EmployeeSocialIns"]);
                    retVal.EmployeeMedicalInc = Convert.ToDecimal(dr["LaterInsurance_EmployeeMedicalIns"]);
                    retVal.SocialInc = Convert.ToDecimal(dr["LaterInsurance_SocialIns"]);
                    retVal.MedicalInc = Convert.ToDecimal(dr["LaterInsurance_MedicalIns"]);
                    retVal.MonthChange = Convert.ToDateTime(dr["LaterInsurance_MonthChange"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        
        public bool LaterInsurance_CheckExistEmployee(int EmployeeID, int month, int year)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LaterInsurance_GetEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LaterInsurance_EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            try
            {
                dbConn.Open();
                int Count = Convert.ToInt32(dbCmd.ExecuteScalar());
                retVal = Count > 0 ? true : false;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public int LaterInsurance_Insert(LaterInsuranceInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LaterInsurance_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LaterInsurance_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_PositionID", eInfo.PositionID);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_Date", eInfo.Date);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_BasicFactor", eInfo.BasicFactor);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_PositionFactor", eInfo.PositionFactor);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_OverSeniorityFactor", eInfo.OverSeniorityFactor);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_SeniorityFactor", eInfo.SeniorityFactor);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_Basic", eInfo.BasicSalary);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_Position", eInfo.PositionAllowance);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_OverSeniority", eInfo.OverSeniorityAllowance);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_Seniority", eInfo.SeniorityAllowance);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_CompanyFactor", eInfo.CompanyFactor);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_Company", eInfo.CompanySalary);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_ResponseFactor", eInfo.ResponseFactor);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_Response", eInfo.ResponseSalary);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_TotalDateWork", eInfo.TotalDateWork);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_ToTalDateOutWork", eInfo.TotalDateOutWork);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_TotalLeavePaid", eInfo.TotalLeavePaid);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_TotalLeaveUnPaid", eInfo.TotalLeaveUnPaid);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_OverTime", eInfo.OverTime);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_ProbationFactor", eInfo.ProbationFactor);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_TradeUnion", eInfo.TradeUnion);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_IncomeTax", eInfo.IncomeTax);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_Lunch", eInfo.CompanyLunch);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_TelephoneCost", eInfo.TelephoneCost);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_OtherResponse", eInfo.OtherResponse);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_OtherDeduction", eInfo.OtherDeduction);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_EmployeeSocialIns", eInfo.EmployeeSocialInc);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_EmployeeMedicalIns", eInfo.EmployeeMedicalInc);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_SocialIns", eInfo.SocialInc);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_MedicalIns", eInfo.MedicalInc);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_MonthChange", eInfo.MonthChange);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool LaterInsurance_Update(LaterInsuranceInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LaterInsurance_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LaterInsurance_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_PositionID", eInfo.PositionID);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_Date", eInfo.Date);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_BasicFactor", eInfo.BasicFactor);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_PositionFactor", eInfo.PositionFactor);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_OverSeniorityFactor", eInfo.OverSeniorityFactor);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_SeniorityFactor", eInfo.SeniorityFactor);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_Basic", eInfo.BasicSalary);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_Position", eInfo.PositionAllowance);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_OverSeniority", eInfo.OverSeniorityAllowance);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_Seniority", eInfo.SeniorityAllowance);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_CompanyFactor", eInfo.CompanyFactor);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_Company", eInfo.CompanySalary);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_ResponseFactor", eInfo.ResponseFactor);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_Response", eInfo.ResponseSalary);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_TotalDateWork", eInfo.TotalDateWork);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_ToTalDateOutWork", eInfo.TotalDateOutWork);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_TotalLeavePaid", eInfo.TotalLeavePaid);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_TotalLeaveUnPaid", eInfo.TotalLeaveUnPaid);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_OverTime", eInfo.OverTime);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_ProbationFactor", eInfo.ProbationFactor);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_TradeUnion", eInfo.TradeUnion);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_IncomeTax", eInfo.IncomeTax);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_Lunch", eInfo.CompanyLunch);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_TelephoneCost", eInfo.TelephoneCost);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_OtherResponse", eInfo.OtherResponse);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_OtherDeduction", eInfo.OtherDeduction);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_EmployeeSocialIns", eInfo.EmployeeSocialInc);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_EmployeeMedicalIns", eInfo.EmployeeMedicalInc);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_SocialIns", eInfo.SocialInc);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_MedicalIns", eInfo.MedicalInc);
            dbCmd.Parameters.AddWithValue("@LaterInsurance_MonthChange", eInfo.MonthChange);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
